WebSocket = window.WebSocket || window.MozWebSocket;
if( !WebSocket ) {
	var wsobg = {};
	var ws_ready = false;
	$(window).load(function(){
		ws_ready = true;
	});
	$(function(){		
		$.ajax({
			url: WS_STATIC_URL + '/jquery.swfobject.js',
			dataType: 'script',
			async: false,
			cache: true
		});
		
		window.WebSocket = function( a ) {
			a = a.match( /wss{0,1}\:\/\/([0-9a-z_.-]+)(?:\:(\d+)){0,1}/i );
			this.host = a[1];
			this.port = a[2] || 843;
			this.onopen = function(){}
			this.onclose = function(){}
			this.onmessage = function(){}
			this.onerror = function(){}
			this.ready = function(b){
				return true;
			}
			this.send = function(b){
				return wsobg.call.Send(b);
			}
			this.close = function(){
				return wsobg.call.Close();
			}
			this.ping = function(){
				return wsobg.call.Ping();
			}
			this.connect = function(){
				wsobg.call = $('#flash_websocket')[0];
				wsobg.call.Connect( this.host,this.port );
			};
			if ( $('#websocket').size() ) {
				this.connect();
			} else {
				
				var divss = $('<div></div>').attr({id:'websocket'}).css({position:'absolute', top:-999, left:-999});
				divss.flash({
					swf: WS_STATIC_URL +'/websocket/websocket.swf',
					wmode: "window",
					scale: "showall",
					allowFullscreen : true,
					allowScriptAccess : 'always',
					id: 'flash_websocket',
					width : 1,
					height : 1,
					flashvars : { call: 'wsobg._this' }
				});
				$('body').append(divss);
			}
			wsobg._this = this;
		}
	});
}

$(function($) {
	$.ws = { obg: {}, message: {}, open:[], close:[], status: false, link:function(){} };
	
	// 添加消息回调函数 1 参数 消息 keys 2参数 回调函数
	$.wsmessage = function( k, f ) {
		if ( !k || !$.isFunction( f ) ) {
			return false;
		}
		$.ws.message[k] = $.ws.message[k] || [];
		$.ws.message[k].push(f);
	};
	
	// 注册 打开回调函数
	$.wsopen = function( f ) {
		if ( !$.isFunction( f ) ) {
			return false;
		}
		$.ws.open.push(f);
	};
	
	// 注册 关闭回调函数
	$.wsclose = function( f ) {
		if ( !$.isFunction( f ) ) {
			return false;
		}
		$.ws.close.push(f);
	};
	
	// 注册 发送信息
	$.wssend = function( d ) {
		return $.ws.status && $.ws.obg.send(d);
	};
	
	$.ws.link = function () {
		$.ws.obg = new WebSocket('ws://' + WS_HOST + ':'+ WS_PORT +'/');
		$.ws.obg.onopen = function(){
			$.ws.status = true;
			$.each( $.ws.open,function( k, v ) {
				v.call(this);
			})
		};
		
		// 关闭
		$.ws.obg.onclose = function() {
			$.ws.status = false;
			$.each( $.ws.close,function( k, v ) {
				v.call(this);
			})
		};
		
		// 接收消息
		/*
		$.ws.obg.onmessage = function( msg ) {
			var d = $.parseJSON( msg.data );
			alert(msg.data);	//消息内容
			d = d || [];
			$.each( d,function( k, v ) {
				$.ws.message[k] = $.ws.message[k] || [];
				$.each( $.ws.message[k],function( kk, vv ) {
					vv.call( this, v );
				})
			})
		};
		*/
		$.ws.obg.onmessage = function( msg ) {
			//alert(msg.data);
			var data = $.parseJSON('['+msg.data+']');
			if(data[0].type == 'ping'){
				$.wssend('{"type":"pong"}');
			}
			if(data[0].type == 'relogin'){
				//alert(msg.data);
			}
			if(data[0].type == 'login'){//登录
				//alert(msg.data);
				if(data[0].error){
					if(data[0].error =='1'){
						 location.href='../?act=ruser';
					}else if(data[0].error =='2'){
						location.href= window.location.pathname;
					}else if(data[0].error =='3'){
						 location.href='../?act=ruser&met=logincg';
					}
				}else{
					if(data[0].message == 'yhdj'){
						alert('账户被冻结，联系客服解冻');
					}else if(data[0].message == 'zmcw'){
						alert('账号或密码错误');
					}else if(data[0].message == 'xtyc'){
						alert('系统异常');
					}
				}
			}
			//{"type":"talk","id":"3","name":"15137932284","content":"15137932284:dsfsd","time":"2015-12-18 23:20:11"}
			if(data[0].type == 'talk'){ //聊天
				//alert(msg.data);
				editor.html('');
				if(data[0].content){
					var neirong = data[0].content;
					var da=$("#jilu").html();
					if(da != ''){
						if(data[0].dq){  //当前用户
							$("#jilu").html(da+'<div class="t_m10 l_m15"><p class="c_00A0E8">'+data[0].name+'&nbsp;'+data[0].time+'</p><p class="c_000">'+neirong+'</p></div>');
						}else{
							$("#jilu").html(da+'<div class="t_m10 l_m15"><p class="c_087906">'+data[0].name+'&nbsp;'+data[0].time+'</p><p class="c_000">'+neirong+'</p></div>');
						}
						
					}else{
						if(data[0].dq){  //当前用户
							$("#jilu").html(da+'<div class="t_m10 l_m15"><p class="c_00A0E8">'+data[0].name+'&nbsp;'+data[0].time+'</p><p class="c_000">'+neirong+'</p></div>');
						}else{
							$("#jilu").html(da+'<div class="t_m10 l_m15"><p class="c_087906">'+data[0].name+'&nbsp;'+data[0].time+'</p><p class="c_000">'+neirong+'</p></div>');
						}
					}
					document.getElementById( 'jilu').scrollTop=document.getElementById( 'jilu').scrollHeight;
				}else{
					if(data[0].error){
						if(data[0].error == 'qxdl'){
							alert('掉线了！刷新重试');
						}else if(data[0].error == 'nrwk'){
							alert('内容不能为空');
						}else if(data[0].error == 'bzx'){
							alert('对方不在线，可能收不到您的消息哦');
						}else if(data[0].error == 'error'){
							alert('请求异常');
						}else if(data[0].error == 'ltdx'){
							alert('请选择聊天对象');
						}else{
							alert('繁忙，刷新一次');
						}
					}else{
						alert('服务异常');
					}
				}
			}
		};
	};
	
	// 连接
	$.ws.link();
	
	
	// 关闭自动重新连接
	$.wsclose(function(){
		setTimeout( $.ws.link, 6000 );
	});
	
	
/*	// 定时 time
	$.wsopen(function(){
		//$.ws.time = setInterval( function(){ $.wssend( "time=ture" ); }, 30000 );
	});
	$.wsclose(function(){
		$.ws.time && clearInterval( $.ws.time );
	});*/

});